home *** CD-ROM | disk | FTP | other *** search
- package
- {
- import Common.ResourceManager;
- import flash.display.BitmapData;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.utils.getDefinitionByName;
-
- public class PartTemplatePackage
- {
-
- internal static const SHOE_SCALE_CUTTOFF:int = 150;
-
- internal static const BOUNDARY_SCALE:Number = 0.4375;
-
-
- internal var _fnCallback:Function;
-
- public var Rules:Array;
-
- public var Templates:Array;
-
- internal var _sPackageFilename:String;
-
- public var Poses:Array;
-
- internal var _xmlresmanager:ResourceManager;
-
- internal var _resmanager:ResourceManager;
-
- public function PartTemplatePackage()
- {
- Templates = new Array();
- Poses = new Array();
- Rules = new Array();
- _xmlresmanager = new ResourceManager();
- _resmanager = new ResourceManager();
- super();
- }
-
- public function loadFile(param1:String, param2:Function) : void
- {
- _sPackageFilename = param1;
- _fnCallback = param2;
- _xmlresmanager.CallbackFunction = onXMLLoaded;
- _resmanager.CallbackFunction = onResLoaded;
- _xmlresmanager.queueResourceLoad(_sPackageFilename,"Package",ResourceManager.C_XML);
- _xmlresmanager.loadResource();
- }
-
- public function getMatchList(param1:PartTemplate) : Array
- {
- var _loc2_:Array = null;
- var _loc3_:Array = null;
- var _loc4_:String = null;
- var _loc5_:PartTemplate = null;
- var _loc6_:Object = null;
- _loc2_ = new Array();
- _loc3_ = new Array();
- _loc4_ = param1.Category;
- for each(_loc5_ in Templates)
- {
- if(_loc5_.Category == _loc4_ && _loc5_ != param1)
- {
- (_loc6_ = new Object()).Item = _loc5_;
- _loc6_.MatchScore = param1.getNumMatch(_loc5_);
- _loc3_.push(_loc6_);
- }
- }
- _loc3_.sortOn("MatchScore",Array.DESCENDING);
- for each(_loc6_ in _loc3_)
- {
- _loc2_.push(_loc6_.Item);
- }
- return _loc2_;
- }
-
- public function getAllTemplates(param1:String) : Array
- {
- var _loc2_:Array = null;
- var _loc3_:int = 0;
- var _loc4_:PartTemplate = null;
- _loc2_ = new Array();
- for each(_loc4_ in Templates)
- {
- if(_loc4_.Category == param1)
- {
- _loc2_.push(_loc4_);
- }
- }
- return _loc2_;
- }
-
- public function getMultipleRandomTemplates(param1:String, param2:int) : Array
- {
- var _loc3_:Array = null;
- var _loc4_:int = 0;
- var _loc5_:PartTemplate = null;
- var _loc6_:Number = NaN;
- _loc3_ = new Array();
- for each(_loc5_ in Templates)
- {
- if(_loc5_.Category == param1)
- {
- _loc3_.push(_loc5_);
- }
- else if(_loc5_.Category == "dress" && param1 == "top")
- {
- _loc3_.push(_loc5_);
- }
- }
- DressupLevelInfo.shuffleArray(_loc3_);
- return _loc3_.slice(0,param2);
- }
-
- public function getTemplateByName(param1:String) : PartTemplate
- {
- var _loc2_:PartTemplate = null;
- for each(_loc2_ in Templates)
- {
- if(_loc2_.Name == param1)
- {
- return _loc2_;
- }
- }
- trace("Part not found : " + param1);
- return null;
- }
-
- public function getRandomTemplate(param1:String) : PartTemplate
- {
- var _loc2_:PartTemplate = null;
- var _loc3_:int = 0;
- var _loc4_:int = 0;
- var _loc5_:int = 0;
- _loc3_ = 0;
- _loc4_ = 0;
- while(_loc4_ < Templates.length)
- {
- _loc2_ = Templates[_loc4_] as PartTemplate;
- if(_loc2_.Category == param1 || _loc2_.Category == "dress" && param1 == "top")
- {
- _loc3_++;
- }
- _loc4_++;
- }
- _loc5_ = Math.random() * _loc3_ + 1;
- _loc4_ = 0;
- while(_loc4_ < Templates.length)
- {
- _loc2_ = Templates[_loc4_] as PartTemplate;
- if(_loc2_.Category == param1 || _loc2_.Category == "dress" && param1 == "top")
- {
- if(--_loc5_ <= 0)
- {
- return _loc2_;
- }
- }
- _loc4_++;
- }
- return null;
- }
-
- protected function createBitmapData(param1:BitmapData, param2:int, param3:Array, param4:Number) : BitmapData
- {
- var _loc5_:Rectangle = null;
- var _loc6_:BitmapData = null;
- var _loc7_:int = 0;
- var _loc8_:int = 0;
- _loc5_ = param3[param2] as Rectangle;
- _loc6_ = new BitmapData(_loc5_.width * param4,_loc5_.height * param4,true,0);
- _loc7_ = 0;
- _loc8_ = 1;
- while(_loc8_ < param2)
- {
- _loc7_ += param3[_loc8_].width;
- _loc8_++;
- }
- _loc6_.copyPixels(param1,new Rectangle(_loc7_ * param4,0,_loc5_.width * param4,_loc5_.height * param4),new Point());
- return _loc6_;
- }
-
- public function getFractionDone() : Number
- {
- if(_resmanager.TotalQueued == 0)
- {
- return 0;
- }
- return Number(_resmanager.TotalLoaded) / Number(_resmanager.TotalQueued);
- }
-
- protected function onXMLLoaded() : void
- {
- var xmlRoot:XML = null;
- var sName:* = undefined;
- var sType:* = undefined;
- var sLayer:* = undefined;
- var sX:* = undefined;
- var sWidth:* = undefined;
- var node:XML = null;
- var nodePose:XML = null;
- var nodeRule:XML = null;
- var nodeRuleAttribute:XML = null;
- var nodeAttribute:XML = null;
- var aAttributes:Array = null;
- var sAttributeName:String = null;
- var iAttributeScore:int = 0;
- var partattribute:PartAttribute = null;
- var aBoundaries:Array = null;
- var aPoseAdjustments:Array = null;
- var sFront:String = null;
- var sBack:String = null;
- var sDisplay:String = null;
- var parttemplate:PartTemplate = null;
- var rBounds:Rectangle = null;
- var iPoseNum:int = 0;
- var rule:Rule = null;
- var sRuleAttributeName:String = null;
- var sAttributeCategory:String = null;
- trace("Package " + _sPackageFilename + " config loaded...");
- xmlRoot = _xmlresmanager.getResource("Package",ResourceManager.C_XML);
- for each(node in xmlRoot.elements())
- {
- switch(node.localName())
- {
- case "Item":
- sName = node.@Name;
- sType = node.@Type;
- sLayer = node.@Layer;
- sFront = node.@Front;
- sBack = node.@Back;
- sDisplay = node.@Display;
- if(isNaN(parseInt(sLayer)))
- {
- sLayer = "0";
- trace("Layer was not specified for " + sName);
- }
- aAttributes = new Array();
- aBoundaries = new Array();
- aPoseAdjustments = new Array();
- for each(nodeAttribute in node.elements())
- {
- if(nodeAttribute.localName() == "Attribute")
- {
- sAttributeName = nodeAttribute.@Name;
- if(isNaN(parseInt(nodeAttribute.@Score)))
- {
- iAttributeScore = 0;
- }
- else
- {
- iAttributeScore = parseInt(nodeAttribute.@Score);
- }
- partattribute = new PartAttribute(sAttributeName,iAttributeScore);
- aAttributes.push(partattribute);
- }
- else if(nodeAttribute.localName() == "bounds")
- {
- rBounds = new Rectangle(parseInt(nodeAttribute.@x),parseInt(nodeAttribute.@y),parseInt(nodeAttribute.@width),parseInt(nodeAttribute.@height));
- iPoseNum = parseInt(nodeAttribute.@pose);
- while(aBoundaries.length <= iPoseNum)
- {
- aBoundaries.push(null);
- aPoseAdjustments.push(null);
- }
- aBoundaries[iPoseNum] = rBounds;
- }
- }
- parttemplate = new PartTemplate(sName,sType,parseInt(sLayer),aAttributes,aBoundaries,aPoseAdjustments);
- Templates.push(parttemplate);
- if(sFront != "")
- {
- parttemplate.HasFront = true;
- }
- if(sBack != "")
- {
- parttemplate.HasBack = true;
- }
- if(!(sType == "shoe" || sType == "accessory"))
- {
- if(sDisplay != "")
- {
- }
- }
- break;
- case "Poses":
- for each(nodePose in node.elements())
- {
- sName = nodePose.@Name;
- sX = nodePose.@x;
- sWidth = nodePose.@width;
- Poses.push(new Pose(sName,parseInt(sX),parseInt(sWidth)));
- }
- break;
- case "Rules":
- Rules = new Array();
- for each(nodeRule in node.elements())
- {
- rule = new Rule();
- rule.Name = nodeRule.@name;
- rule.Comment = nodeRule.@comment;
- rule.Score = parseInt(nodeRule.@score);
- rule.Voice = nodeRule.@voice;
- for each(nodeRuleAttribute in nodeRule.elements())
- {
- sRuleAttributeName = nodeRuleAttribute.@name;
- sAttributeCategory = null;
- try
- {
- sAttributeCategory = nodeRuleAttribute.@category;
- }
- catch(exception:Object)
- {
- }
- rule.Attributes.push(new Triple(sRuleAttributeName,sAttributeCategory,null));
- }
- Rules.push(rule);
- }
- trace("Unknown tag " + node.localName());
- break;
- }
- }
- _resmanager.loadResource();
- }
-
- public function getTemplateIndexByName(param1:String) : int
- {
- var _loc2_:int = 0;
- _loc2_ = 0;
- while(_loc2_ < Templates.length)
- {
- if(Templates[_loc2_].Name == param1)
- {
- return _loc2_;
- }
- _loc2_++;
- }
- return -1;
- }
-
- protected function onResLoaded() : void
- {
- var mcBitmap:Class = null;
- var parttemplate:PartTemplate = null;
- var bdComposite:BitmapData = null;
- var bdCompositeFront:BitmapData = null;
- var bdCompositeBack:BitmapData = null;
- var pose:Pose = null;
- var iPoseIndex:int = 0;
- var rSource:Rectangle = null;
- var rBounds:Rectangle = null;
- var bitmapdata:BitmapData = null;
- var nScale:Number = NaN;
- var bitmapdataScaled:BitmapData = null;
- var matrix:Matrix = null;
- var pointAdjustment:Point = null;
- var display_name:String = null;
- trace("Package " + _sPackageFilename + " res loaded...");
- for each(parttemplate in Templates)
- {
- try
- {
- mcBitmap = getDefinitionByName(parttemplate.Name) as Class;
- bdComposite = new mcBitmap(0,0);
- bdCompositeFront = null;
- bdCompositeBack = null;
- if(parttemplate.HasFront)
- {
- mcBitmap = getDefinitionByName(parttemplate.Name + "_front") as Class;
- bdCompositeFront = new mcBitmap(0,0);
- }
- if(parttemplate.HasBack)
- {
- mcBitmap = getDefinitionByName(parttemplate.Name + "_back") as Class;
- bdCompositeBack = new mcBitmap(0,0);
- }
- for each(pose in Poses)
- {
- iPoseIndex = parseInt(pose.Name);
- rSource = new Rectangle();
- rBounds = parttemplate.Boundaries[iPoseIndex] as Rectangle;
- bitmapdata = createBitmapData(bdComposite,iPoseIndex,parttemplate.Boundaries,BOUNDARY_SCALE);
- nScale = BOUNDARY_SCALE;
- matrix = new Matrix();
- matrix.scale(nScale,nScale);
- bitmapdataScaled = bitmapdata;
- parttemplate.setBitmapData(pose.Name,bitmapdataScaled);
- if(bdCompositeFront != null)
- {
- bitmapdata = createBitmapData(bdCompositeFront,iPoseIndex,parttemplate.Boundaries,BOUNDARY_SCALE);
- bitmapdataScaled = bitmapdata;
- parttemplate.setBitmapDataFront(pose.Name,bitmapdataScaled);
- }
- if(bdCompositeBack != null)
- {
- bitmapdata = createBitmapData(bdCompositeBack,iPoseIndex,parttemplate.Boundaries,BOUNDARY_SCALE);
- bitmapdataScaled = bitmapdata;
- parttemplate.setBitmapDataBack(pose.Name,bitmapdataScaled);
- }
- pointAdjustment = new Point();
- pointAdjustment.x = (rBounds.x - (pose.x - pose.width / 2)) * nScale;
- pointAdjustment.y = rBounds.y * nScale;
- parttemplate.PoseAdjustments[iPoseIndex] = pointAdjustment;
- if(parttemplate.Category != "shoe" && pose.Name == ModelPose.DEFAULT_SOURCE || parttemplate.Category == "shoe" && pose.Name == ModelPose.DEFAULT_SHOE_SOURCE)
- {
- if(parttemplate.Category == "body")
- {
- nScale = 0.2 / BOUNDARY_SCALE;
- }
- else if(parttemplate.Category == "dress")
- {
- nScale = 0.3 / BOUNDARY_SCALE;
- }
- else
- {
- nScale = 0.5 / BOUNDARY_SCALE;
- }
- bitmapdataScaled = new BitmapData(bitmapdata.width * nScale,bitmapdata.height * nScale,true,0);
- matrix = new Matrix();
- matrix.scale(nScale,nScale);
- bitmapdataScaled.draw(bitmapdata,matrix,null,null,null,true);
- parttemplate.setBitmapData(ModelPose.DEFAULT,bitmapdataScaled);
- }
- }
- if(bdCompositeFront != null)
- {
- bdCompositeFront.dispose();
- bdCompositeFront = null;
- }
- if(bdCompositeBack != null)
- {
- bdCompositeBack.dispose();
- bdCompositeBack = null;
- }
- bdComposite.dispose();
- bdComposite = null;
- }
- catch(ex:Object)
- {
- trace("*****************Error loading: " + parttemplate.Name);
- }
- try
- {
- display_name = parttemplate.Name + "_display";
- if(getDefinitionByName(display_name) != null)
- {
- mcBitmap = getDefinitionByName(display_name) as Class;
- bitmapdata = new mcBitmap(0,0);
- if(bitmapdata.height < SHOE_SCALE_CUTTOFF)
- {
- nScale = 1;
- }
- else
- {
- nScale = 0.5;
- }
- bitmapdataScaled = new BitmapData(bitmapdata.width * nScale,bitmapdata.height * nScale,true,0);
- matrix = new Matrix();
- matrix.scale(nScale,nScale);
- bitmapdataScaled.draw(bitmapdata,matrix,null,null,null,true);
- parttemplate.setBitmapData(ModelPose.DEFAULT,bitmapdataScaled);
- parttemplate.DefaultBitmapProvided = true;
- bitmapdata.dispose();
- bitmapdata = null;
- }
- }
- catch(ex:Object)
- {
- trace("Error parsing bitmap " + parttemplate.Name + "\n" + ex.toString());
- }
- }
- _xmlresmanager.purgeAllResources();
- _resmanager.purgeAllResources();
- _fnCallback(_sPackageFilename);
- }
-
- public function loadData(param1:String, param2:Function) : void
- {
- _sPackageFilename = param1;
- _fnCallback = param2;
- _xmlresmanager.CallbackFunction = onXMLLoaded;
- _resmanager.CallbackFunction = onResLoaded;
- _xmlresmanager.setXMLResource("Package",param1);
- onXMLLoaded();
- }
-
- public function copy(param1:PartTemplatePackage) : void
- {
- var _loc2_:PartTemplate = null;
- for each(_loc2_ in param1.Templates)
- {
- Templates.push(_loc2_);
- }
- Poses = param1.Poses;
- Rules = param1.Rules;
- }
-
- public function filter(param1:Array) : void
- {
- var _loc2_:Array = null;
- var _loc3_:PartTemplate = null;
- var _loc4_:String = null;
- _loc2_ = new Array();
- for each(_loc3_ in Templates)
- {
- if(_loc3_.Layer == 0)
- {
- _loc2_.push(_loc3_);
- }
- else
- {
- for each(_loc4_ in param1)
- {
- if(_loc3_.Name == _loc4_)
- {
- _loc2_.push(_loc3_);
- break;
- }
- }
- }
- }
- Templates = _loc2_;
- }
-
- protected function trimBitmapData(param1:BitmapData, param2:Boolean = false) : BitmapData
- {
- var _loc3_:BitmapData = null;
- var _loc4_:Rectangle = null;
- var _loc5_:Rectangle = null;
- var _loc6_:Point = null;
- var _loc7_:int = 0;
- var _loc8_:int = 0;
- _loc4_ = new Rectangle();
- _loc5_ = new Rectangle();
- _loc6_ = new Point();
- _loc4_.x = 0;
- _loc4_.y = 0;
- _loc4_.width = param1.width;
- _loc4_.height = 1;
- if(param2)
- {
- _loc7_ = 0;
- while(_loc7_ < param1.height)
- {
- _loc4_.y = _loc7_;
- if(param1.hitTest(_loc6_,10,_loc4_))
- {
- _loc5_.y = _loc7_;
- break;
- }
- _loc7_++;
- }
- }
- _loc7_ = param1.height - 1;
- while(_loc7_ >= 0)
- {
- _loc4_.y = _loc7_;
- if(param1.hitTest(_loc6_,10,_loc4_))
- {
- _loc5_.height = _loc7_ - _loc5_.y;
- break;
- }
- _loc7_--;
- }
- _loc4_.x = 0;
- _loc4_.y = 0;
- _loc4_.width = 1;
- _loc4_.height = param1.height;
- if(param2)
- {
- _loc7_ = 0;
- while(_loc7_ < param1.width)
- {
- _loc4_.x = _loc7_;
- if(param1.hitTest(_loc6_,10,_loc4_))
- {
- _loc5_.x = _loc7_;
- break;
- }
- _loc7_++;
- }
- }
- _loc7_ = param1.width - 1;
- while(_loc7_ >= 0)
- {
- _loc4_.x = _loc7_;
- if(param1.hitTest(_loc6_,10,_loc4_))
- {
- _loc5_.width = _loc7_ - _loc5_.x;
- break;
- }
- _loc7_--;
- }
- _loc3_ = new BitmapData(_loc5_.width,_loc5_.height);
- _loc3_.copyPixels(param1,_loc5_,new Point());
- return _loc3_;
- }
- }
- }
-